package com.appcheck.recording.listener.impl;

import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;

import com.appcheck.recording.ListenerManager;
import com.appcheck.recording.WidgetHelper;
import com.appcheck.recording.action.impl.ScrollListAction;
import com.appcheck.recording.action.impl.ScrollStateChangedAction;
import com.appcheck.recording.listener.AbstractListener;
import com.appcheck.recording.utils.Logger;

public class ScrollListener extends AbstractListener implements
		OnScrollListener {
	private int b;
	private View absListView;
	
	public ScrollListener(View paramAbsListView) {
		super(paramAbsListView, AbsListView.class, AbsListView.OnScrollListener.class);
		this.absListView = paramAbsListView;
	}
	/**
	 * 正在滚动时回调，回调2-3次，手指没抛则回调2次。scrollState = 2的这次不回调<br>
	 * 回调顺序如下<br>
	 *	第1次：scrollState = SCROLL_STATE_TOUCH_SCROLL(1) 正在滚动<br>
	 * 	第2次：scrollState = SCROLL_STATE_FLING(2) 手指做了抛的动作（手指离开屏幕前，用力滑了一下）<br>
	 * 	第3次：scrollState = SCROLL_STATE_IDLE(0) 停止滚动
	 */
	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		 ScrollStateChangedAction scrollStateChangedAction = new ScrollStateChangedAction();
		 scrollStateChangedAction.setScrollState(scrollState);
		 ListenerManager.getListenerManager().handleAction(scrollStateChangedAction);
		 if(this.listener != null){
			 ((AbsListView.OnScrollListener)this.listener).onScrollStateChanged(view, scrollState);
		 }
	}
	/**
	 * 滚动时一直回调，直到停止滚动时才停止回调。单击时回调一次。<br>
	 * firstVisibleItem：当前能看见的第一个列表项ID（从0开始）<br>
	 * visibleItemCount：当前能看见的列表项个数（小半个也算）<br>
	 * totalItemCount：列表项共数
	 */
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
	    if (this.b++ > 1) {
	    	Logger.INFO("call onScroll count : " + b);
	        return;
	    }
	    ScrollListAction scrollListAction = new ScrollListAction();
	    scrollListAction.setLine(firstVisibleItem);
	    scrollListAction.setParentResourceName(WidgetHelper.getResourceName(view));
	    scrollListAction.setParentResourceId(WidgetHelper.getResourceId(view));
	    scrollListAction.setAbsListView(this.absListView);
	    ListenerManager.getListenerManager().handleAction(scrollListAction);
	    if (this.listener != null) {
	       ((AbsListView.OnScrollListener)this.listener).onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
	    }
	    this.b = 0;

	}

}
